package com.xyd.controller;

import com.xyd.entity.User;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@SessionAttributes(value = {"names"}, types = {Integer.class})
@RequestMapping("/test")
@Controller
public class TestController {

    @RequestMapping("/testSessAttrs")
    public String test(Map<String, Object> map) {
        map.put("names", Arrays.asList("zhangsan", "lisi", "wangwu"));
        map.put("age", 22);
        return "test";
    }

    @RequestMapping("/setUname")
    public ModelAndView setUname() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", new User("zhaoliu", "222"));
        mv.setViewName("test");
        return mv;
    }

    @RequestMapping("/getUser/{id}")
    public ModelAndView testRestful(@PathVariable("id") long id) {
        System.out.println("id=====" + id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test");
        return mv;
    }

    @RequestMapping("/testReqBody1")
    public ModelAndView testReqBody1(@RequestBody User user) {
        System.out.println("username===" + user.getUsername());
        System.out.println("roleName===" + user.getRole().getRoleName());
        List<User> list = new ArrayList<>();
        User u1 = new User();
        u1.setUsername("zhangsan");
        u1.setPassword("111");

        User u2 = new User();
        u2.setUsername("lisi");
        u2.setPassword("222");

        User u3 = new User();
        u3.setUsername("wangwu");
        u3.setPassword("333");
        list.add(u1);
        list.add(u2);
        list.add(u3);


        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/delUser")
    public ModelAndView delUser(@RequestBody List<Integer> idList) {
        ModelAndView mv = new ModelAndView();
        System.out.println(idList);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/seriaForm")
    public ModelAndView seriaForm(String roleName, String note) {
        System.out.println("roleName===" + roleName);
        System.out.println("note===" + note);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test");
        return mv;
    }

    @RequestMapping("/getHeaderAndCookie")
    public String testHeaderAndCookie(@RequestHeader(value = "User-Agent", required = false, defaultValue = "attribute") String userAgent,
                                      @CookieValue(value = "JSESSIONID", required = true, defaultValue = "MyJsessionId") String jsessionId) {
        System.out.println("user-Agent==" + userAgent);
        System.out.println("jsessionid==" + jsessionId);
        return "test";
    }
}


